<?php

namespace App\Medicare_Class;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use phpQuery;
use App\Medicare;
use App\Medicare_Theme;
error_reporting(E_ALL^E_NOTICE);

/**
 * 获取给定url的内容
 * @param $url:url地址
 * 
 */
class Medicare_Class
{    
     public static function getGain($url){
        //获取给定url的内容
        $fileContent = file_get_contents($url);
        phpQuery::newDocumentHTML($fileContent,$charset='utf-8');

        //通过id查找同级元素
        $as = pq("#channel53054")->nextAll();

        //遍历获取得到的元素内容
        foreach ($as as $key => $value) {
            //获取元素中的title 医疗主题
            $title = ($value->getAttribute('title'));
            //获取元素中的href  主题路径
            $href = ($value->getAttribute('href'));
            //编码转换
            $title = mb_convert_encoding($title,'ISO-8859-1','utf-8');
            $title = mb_convert_encoding($title,'utf-8','GBK');

            //调用format_url进行相对路径转换
            $baseurl = 'http://www.szsi.gov.cn/gzfw/ywzsk/ylbx/jbgn/asd/';
            $href = self::format_url($href, $baseurl);

            //将医疗主题与获得时间存入到数据库里
            $med = new Medicare_Theme();
            $med->theme = $title;
            $res = $med->save();

            //判断执行结果
            if($res) {
                echo "$title-->主题添加成功\r\n";

                self::issue($href,$title); //添加成功调用issue方法 获取问题标题与答案路径
            } else {
                echo "$title-->主题添加失败\r\n";
            }

        }

    }

    /**
     *获取问题标题和答案路径
     * @param $theme_url:主题的url
     * @param  $title:主题标题
     *
     */
    static function issue($theme_url,$title) {
        //获取问题的htlm
        $titleContent = file_get_contents($theme_url);
        phpQuery::newDocumentHTML($titleContent,$charset='utf-8');

        //获取class为ftdt-list下的a标签
        $issue = pq('ul.ftdt-list a');


        foreach ($issue as $key => $value) {
            //问题的url
            $issue_url = ($value->getAttribute('href'));
            //问题的标题
            $issue_title = ($value->getAttribute('title'));
            //编码转换
            $issue_title = mb_convert_encoding($issue_title,'ISO-8859-1','utf-8');
            $issue_title = mb_convert_encoding($issue_title,'utf-8','GBK');
            //调用format_url进行路径转换
            $baseurl = $theme_url.'asd/';
            $issue_url = self::format_url($issue_url, $baseurl);

            //调用answer方法获取答案内容
            self::answer($issue_url,$issue_title,$title);
        }
    }

    /**
     * 获取答案的标题和内容
     *@param $answer_url:答案的地址
     *@param $issue_title:问题标题
     *@param $title:主题的标题
     */
    static function answer($answer_url,$issue_title,$title) {
        //获取给定url的内容
        $fileContent = file_get_contents($answer_url);
        phpQuery::newDocumentHTML($fileContent,$charset='utf-8');

        //获取class为newsCon下的内容
        $answer = pq(".newsCon")->text();

        //编码转换 问题答案
        $answer = mb_convert_encoding($answer,'ISO-8859-1','utf-8');
        $answer = mb_convert_encoding($answer,'utf-8','GBK');

        //查询主题id
        $data = Medicare_Theme::where('theme','=',$title)->first();
        $theme_id = $data['id'];

        // 准备sql 将问题的标题与答案存入到数据库里
        $data = new Medicare();
        $data->theme_id = $theme_id;
        $data->question = $issue_title;
        $data->answer = $answer;
        $res = $data->save();

        //判断执行结果
        if($res) {
            echo "$issue_title--->答案添加成功\r\n";

           sleep(0.1);//延迟20秒执行
        } else {
            echo "$issue_title--->答案添加成功\r\n";
        }

    }

    /**
     * 将一个URL转换为完整URL
     * ../a.php or abc/a.php => http:abc/a.php
     *
     */
    static public function format_url($srcurl, $baseurl) {
        $srcinfo = parse_url($srcurl);
        if(isset($srcinfo['scheme'])) {
            return $srcurl;
        }
        $baseinfo = parse_url($baseurl);
        $url = $baseinfo['scheme'].'://'.$baseinfo['host'];
        if(substr($srcinfo['path'], 0, 1) == '/') {
            $path = $srcinfo['path'];
        }else{
            $path = dirname($baseinfo['path']).'/'.$srcinfo['path'];
        }
        $rst = array();
        $path_array = explode('/', $path);
        if(!$path_array[0]) {
            $rst[] = '';
        }
        foreach ($path_array AS $key => $dir) {
            if ($dir == '..') {
                if (end($rst) == '..') {
                    $rst[] = '..';
                }elseif(!array_pop($rst)) {
                    $rst[] = '..';
                }
            }elseif($dir && $dir != '.') {
                $rst[] = $dir;
            }
        }
        if(!end($path_array)) {
            $rst[] = '';
        }
        $url .= implode('/', $rst);
        return str_replace('\\', '/', $url);
    }
}
